home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / ProgressDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-28  |  3.6 KB  |  138 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <windows.h>
  19. #include <commctrl.h>
  20.  
  21. #include "resource.h"
  22.  
  23. #include "ProgressDialog.h"
  24.  
  25. #include "Error.h"
  26.  
  27. extern HINSTANCE g_hInst;
  28.  
  29. ProgressDialog::ProgressDialog(HWND hwndParent, const char *szTitle, const char *szCaption, long maxval, bool fAbortEnabled) {
  30.     lpszTitle        = szTitle;
  31.     lpszCaption        = szCaption;
  32.     lpszValueFormat    = NULL;
  33.     this->maxval    = maxval;
  34.     this->curval    = 0;
  35.     this->newval    = 0;
  36.     this->fAbortEnabled = fAbortEnabled;
  37.  
  38.     fAbort = false;
  39.  
  40.     hwndProgressBar = NULL;
  41.     hwndValue        = NULL;
  42.     hwndDialog        = NULL;
  43.  
  44.     CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_PROGRESS), hwndParent, ProgressDlgProc, (LPARAM)this);
  45. }
  46.  
  47. ProgressDialog::~ProgressDialog() {
  48.     close();
  49. }
  50.  
  51. void ProgressDialog::setValueFormat(const char *sz) {
  52.     lpszValueFormat = sz;
  53. }
  54.  
  55. #if 0
  56. void ProgressDialog::advance(long newval) {
  57.     if (!hwndProgressBar) return;
  58.  
  59.     if (newval > curval) {
  60.         curval = newval;
  61.         if (curval > maxval) curval = maxval;
  62.  
  63.         SendMessage(hwndProgressBar, PBM_SETPOS, (WPARAM)MulDiv(curval, 16384, maxval), 0);
  64.     }
  65. }
  66. #endif
  67.  
  68. void ProgressDialog::check() {
  69.     MSG msg;
  70.  
  71.     while(PeekMessage(&msg, hwndDialog, 0, 0, PM_REMOVE)) {
  72.         if (!IsWindow(hwndDialog) || !IsDialogMessage(hwndDialog, &msg)) {
  73.             TranslateMessage(&msg);
  74.             DispatchMessage(&msg);
  75.         }
  76.     }
  77.  
  78.     if (fAbort)
  79.         throw MyUserAbortError();
  80. }
  81.  
  82. void ProgressDialog::close() {
  83.     if (hwndDialog)
  84.         DestroyWindow(hwndDialog);
  85. }
  86.  
  87. BOOL CALLBACK ProgressDialog::ProgressDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
  88.     ProgressDialog *thisPtr = (ProgressDialog *)GetWindowLong(hDlg, DWL_USER);
  89.     int newval2;
  90.  
  91.     switch(msg) {
  92.         case WM_INITDIALOG:
  93.             SetWindowLong(hDlg, DWL_USER, lParam);
  94.  
  95.             thisPtr = (ProgressDialog *)lParam;
  96.             thisPtr->hwndProgressBar = GetDlgItem(hDlg, IDC_PROGRESS);
  97.             thisPtr->hwndValue = GetDlgItem(hDlg, IDC_CURRENT_VALUE);
  98.             SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), PBM_SETRANGE, 0, MAKELPARAM(0, 16384));
  99.  
  100.             if (!thisPtr->fAbortEnabled)
  101.                 EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);
  102.  
  103.             SetWindowText(hDlg, thisPtr->lpszTitle);
  104.             SetDlgItemText(hDlg, IDC_STATIC_MESSAGE, thisPtr->lpszCaption);
  105.  
  106.             thisPtr->hwndDialog = hDlg;
  107.  
  108.             SetTimer(hDlg, 1, 500, NULL);
  109.  
  110.             break;
  111.  
  112.         case WM_TIMER:
  113.             newval2 = MulDiv(thisPtr->newval, 16384, thisPtr->maxval);
  114.  
  115.             if (newval2 > thisPtr->curval) {
  116.                 if (newval2 > 16384) newval2 = 16384;
  117.                 thisPtr->curval = newval2;
  118.  
  119.                 SendMessage(thisPtr->hwndProgressBar, PBM_SETPOS, (WPARAM)newval2, 0);
  120.             }
  121.  
  122.             if (thisPtr->lpszValueFormat) {
  123.                 char szTemp[128];
  124.  
  125.                 wsprintf(szTemp, thisPtr->lpszValueFormat, thisPtr->newval, thisPtr->maxval);
  126.                 SendMessage(thisPtr->hwndValue, WM_SETTEXT, 0, (LPARAM)szTemp);
  127.             }
  128.             return TRUE;
  129.  
  130.         case WM_COMMAND:
  131.             if (LOWORD(wParam) == IDCANCEL)
  132.                 thisPtr->fAbort = true;
  133.             return TRUE;
  134.     }
  135.  
  136.     return FALSE;
  137. }
  138.